package com.example.baselibs.base.activity;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProviders;

import com.alibaba.android.arouter.launcher.ARouter;
import com.example.baselibs.R;
import com.example.baselibs.base.BaseToolBarFun;
import com.example.baselibs.utils.ActivityManager;
import com.example.baselibs.utils.ActivityUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseToolBarActivity extends AppCompatActivity implements BaseToolBarFun {
    FrameLayout frameLayout;
    TextView textView;
    LayoutInflater inflater;
    //Butterknife绑定
    private Unbinder unbinder;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflater=LayoutInflater.from(this);
        ViewGroup rootView=findViewById(android.R.id.content);
        inflater.inflate(R.layout.layout_base_toolbar, rootView,true);
        frameLayout=findViewById(R.id.fl_content);
        inflater.inflate(getLayoutRes(),frameLayout,true);
        textView=findViewById(R.id.tv_title);
        textView.setText(setTitle());
        initButterKnife();
        initData(savedInstanceState);
        initARouter();
        ActivityManager.getInstance().addActivity(this);
    }

    /**
     * 初始化Butterknife
     */
    private void initButterKnife(){
        unbinder= ButterKnife.bind(this);
    }
    /**
     * 调用 inject 方法，如果传递过来的参数含有，这样使用 @Autowired 的会自动解析
     */
    private void initARouter(){
        ARouter.getInstance().inject(this);
    }
    public void toActivity(String path){
        ARouter.getInstance().build(path).navigation();
    }

    /**
     * 获取ViewModel
     * @param clz
     * @param <T>
     * @return
     */
    public <T extends ViewModel> T getViewModel(Class<T> clz){
        return ViewModelProviders.of(this).get(clz);
    }

    /**
     * 简化Toast
     * @param msg
     */
    public void showToast(String msg){
        ActivityUtils.showToast(this,msg);
    }


    public void onClick(View v){
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        ActivityManager.getInstance().removeActivity(this);
    }
}
